package com.itheima.health.service.impl;
import com.itheima.health.dao.SendDao;
import com.itheima.health.dto.SmsLoginParam;
import com.itheima.health.dto.VeiflcationcodeParam;
import com.itheima.health.service.SendService;
import com.itheima.health.util.AliyunSmsUtil;
import com.itheima.health.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class SendServiceImpl implements SendService {
    @Autowired
    private SendDao sendDao;

    @Override
    public void send(VeiflcationcodeParam veiflcationcodeParam) {
        //随机4位数验证码，我这里使用的hutool随机数工具类进行生成
        String code = RandomUtil.randomNumbers(4);
        //获取手机号
        String telephone = veiflcationcodeParam.getTelephone();
        //封装工具类所需参数
        Map<String,String> map = new HashMap<>();
        map.put("phoneNumbers",telephone);
        map.put("signName","邹志林的云医疗");
        map.put("templateCode","SMS_460785777");
        //调用工具类完成短信发送
        String success = AliyunSmsUtil.sendSms(map,code);
        //验证短信发送是否成功
        if(success !=null){
            System.out.println("短信发送成功");
        }else{
            System.out.println("短信发送失败");
        }

        //先查询数据库看看此手机号已经登录,根据手机好查询
       VeiflcationcodeParam v =  sendDao.seletDoes(veiflcationcodeParam);
        if (v !=null){
            //修改此数据 ，添加一个验证码
            veiflcationcodeParam.setAddTime(LocalDateTime.now());
            veiflcationcodeParam.setValidateCode(success);
            sendDao.savecode(veiflcationcodeParam);
        }
        //添加一个数据
        if (v == null){
            veiflcationcodeParam.setValidateCode(success);
            veiflcationcodeParam.setAddTime(LocalDateTime.now());
            sendDao.addcode(veiflcationcodeParam);
        }

    }
/*
* 查询数据库实现登录
* */
    @Override
    public VeiflcationcodeParam longin(SmsLoginParam smsLoginParam) {
       VeiflcationcodeParam veiflcationcodeParam = sendDao.selectlongin(smsLoginParam);
        return veiflcationcodeParam;
    }
}

